package com.leyou.item.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;


    public PageResult<Brand> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //开始分页
        PageHelper.startPage(page, rows);
        Example example = new Example(Brand.class);
        //有搜索条件了模糊查询
        if (StringUtils.isNotBlank(key)) {
            //品牌名字和首字母，，模糊查询
            example.createCriteria().andLike("name", "%" + key + "%")
                    .orEqualTo("letter", key.toUpperCase());
        }
        //sortBy不为空了排序
        if (StringUtils.isNotBlank(sortBy)) {
            //order by id  desc       id存在时desc为true或者false
            String orderBySortBy = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderBySortBy);
        }
        //查询分页品牌的信息列表
        Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);
        return new PageResult<>(brands.getTotal(), brands);

    }

    //新增商品。单表操作。插入后，更新中间表。
    @Transactional//cids为字符串数组。用String cids或者List<Long> cids
    public void saveBrand(Brand brand, List<Long> cids) {
        //新增商品。
        int insert = this.brandMapper.insert(brand);
        //更新中间表
        for (long cid : cids) {
            this.brandMapper.insertCategoryBrand(cid, brand.getId());//由于brand中配置了主键回显，所以可以得到brand的id
        }
    }

    //删除商品
    @Transactional
    public void deleteBrand(long bid) {
        this.brandMapper.deleteByPrimaryKey(bid);//删除商品表
        this.brandMapper.deleteCategoryBrand(bid);//删除中间表
    }

    //修改商品。bid 不变。
    public void updateBrand(Brand brand, List<Long> cids) {
        this.brandMapper.updateByPrimaryKey(brand);//修改商品表
        //先删除在添加
        this.brandMapper.deleteCategoryBrand(brand.getId());//由于brand中配置了主键回显，所以可以得到brand的id
        for (long cid : cids) {
            this.brandMapper.insertCategoryBrand(cid, brand.getId());//由于brand中配置了主键回显，所以可以得到brand的id
        }
    }

    //通过id查询品牌
    public Brand queryBrandById(Long brandId) {
        return this.brandMapper.selectByPrimaryKey(brandId);
    }

    //通过cid 查询商品的信息
    public List<Brand> queryBrandByCategory(Long cid) {
        return this.brandMapper.queryBrandByCategory(cid);
    }

    //通过bid 查询商品的信息名称
    public String queryBrandNameById(Long bid) {
        Brand brand = this.brandMapper.selectByPrimaryKey(bid);
        return brand.getName();
    }

    //根据商品的ids查询商品的列表
    public List<Brand> queryBrandByIds(List<Long> bids) {
        return this.brandMapper.selectByIdList(bids);
    }
}
